package com.example.myapplication

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import kotlin.math.abs
import androidx.core.graphics.createBitmap

//package com.example.myapplication
//
//import android.annotation.SuppressLint
//import android.content.Context
//import android.graphics.Canvas
//import android.graphics.Color
//import android.graphics.Paint
//import android.graphics.Path
//import android.graphics.RectF
//import android.util.AttributeSet
//import android.view.View
//import androidx.core.graphics.withTranslation
//
//
//class MyView(context: Context, attrs: AttributeSet) : View(context, attrs) {
//    private val mPaint = Paint()
//
//    init {
//        mPaint.isAntiAlias = true //抗锯齿
//        mPaint.color = resources.getColor(R.color.other_color) //画笔颜色
//        mPaint.style = Paint.Style.STROKE //画笔风格
//        mPaint.textSize = 36f //绘制文字大小，单位px
//        mPaint.strokeWidth = 5f //画笔粗细
//    }
//
//    @SuppressLint("DrawAllocation")
//    override fun onDraw(canvas: Canvas) {
//        super.onDraw(canvas)
//        canvas.scale(2f, 2f)
//        canvas.drawColor(resources.getColor(R.color.second_color))   //设置画布背景颜色
//        canvas.drawCircle(0f, 0f, 100F, mPaint);
//
//
//        canvas.translate(width.toFloat() / 3, height.toFloat() / 3) //将位置移动画纸的坐标点:150,150
//        canvas.drawCircle(0f, 0f, 100f, mPaint) //画圆圈
//
//
//        //使用path绘制路径文字
//        canvas.withTranslation(-75f, -75f) {
//            val path = Path()
//            path.addArc(RectF(0f, 0f, 150f, 150f), -180f, 180f)
//            val citePaint = Paint(mPaint)
//            citePaint.textSize = 14f
//            citePaint.strokeWidth = 1f
//            drawTextOnPath("绘制表盘~", path, 28f, 0f, citePaint)
//        }
//
//        val tmpPaint = Paint(mPaint) //小刻度画笔对象
//        tmpPaint.strokeWidth = 1f
//
//        val y = 95f
//        val count = 60 //总刻度数
//
//        for (i in 0..<count) {
//            if (i % 5 == 0) {
//                canvas.drawLine(0f, y, 0f, y + 12f, mPaint)
//                canvas.drawText((i / 5 + 1).toString(), -4f, y + 25f, tmpPaint)
//            } else {
//                canvas.drawLine(0f, y, 0f, y + 5f, tmpPaint)
//            }
//            canvas.rotate((360 / count).toFloat(), 0f, 0f) //旋转画纸
//        }
//
//
//        //绘制指针
//        tmpPaint.color = Color.GRAY
//        tmpPaint.strokeWidth = 4f
//        canvas.drawCircle(0f, 0f, 7f, tmpPaint)
//        tmpPaint.style = Paint.Style.FILL
//        tmpPaint.color = Color.YELLOW
//        canvas.drawCircle(0f, 0f, 5f, tmpPaint)
//        canvas.drawLine(0f, 10f, 0f, -65f, mPaint)
////        canvas.save()
//    }
//}
/**
 * Created by Jay on 2015/10/15 0015.
 */
class MyView : View {
    private lateinit var mPaint: Paint  //绘制线条的Path
    private lateinit var mPath: Path //记录用户绘制的Path
    private lateinit var mCanvas: Canvas //内存中创建的Canvas
    private lateinit var mBitmap: Bitmap //缓存绘制的内容

    private var mLastX = 0.0F
    private var mLastY = 0f

    constructor(context: Context?) : super(context) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        init()
    }

    private fun init() {
        mPath = Path()
        mPaint = Paint() //初始化画笔
        mPaint.color = resources.getColor(R.color.other_color)
        mPaint.isAntiAlias = true
        mPaint.isDither = true
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeJoin = Paint.Join.ROUND //结合处为圆角
        mPaint.strokeCap = Paint.Cap.ROUND // 设置转弯处为圆角
        mPaint.strokeWidth = 20f // 设置画笔宽度
    }

    @SuppressLint("DrawAllocation")
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = measuredWidth
        val height = measuredHeight
        // 初始化bitmap,Canvas
        mBitmap = createBitmap(width, height)
        mCanvas = Canvas(mBitmap)
    }

    //重写该方法，在这里绘图
    override fun onDraw(canvas: Canvas) {
        drawPath()
        canvas.drawBitmap(mBitmap, 0f, 0f, null)
    }

    //绘制线条
    private fun drawPath() {
        mCanvas.drawPath(mPath, mPaint)
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        val action = event.action
        val x = event.x
        val y = event.y

        when (action) {
            MotionEvent.ACTION_DOWN -> {
                mLastX = x
                mLastY = y
                mPath.moveTo(mLastX, mLastY)
            }

            MotionEvent.ACTION_MOVE -> {
                val dx = abs((x - mLastX).toDouble()).toInt()
                val dy = abs((y - mLastY).toDouble()).toInt()
                if (dx > 3 || dy > 3) mPath.lineTo(x, y)
                mLastX = x
                mLastY = y
            }
        }

        invalidate()
        return true
    }
}